package com.example.categorylist;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseExpandableListAdapter;
import android.widget.Toast;

import com.diegocarloslima.fgelv.lib.FloatingGroupExpandableListView;
import com.diegocarloslima.fgelv.lib.WrapperExpandableListAdapter;

public class MainActivity extends Activity {
    
    private MyAdapter mAdapter;
    private LayoutInflater mLayoutInfalter;
    private FloatingGroupExpandableListView mList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.category_list);
        
        mLayoutInfalter = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
        FloatingGroupExpandableListView list = (FloatingGroupExpandableListView) findViewById(R.id.category_list);
        mList = list;
        
        mAdapter = new MyAdapter(this);
        WrapperExpandableListAdapter wrapperAdatper = new WrapperExpandableListAdapter(mAdapter);
        list.setAdapter(wrapperAdatper);
    }
    
    class MyAdapter extends BaseExpandableListAdapter {
        
    	private final String[] mGroups = {
    			"Cupcake",
    			"Donut",
    			"Eclair",
    			"Froyo",
    			"Gingerbread",
    			"Honeycomb",
    			"Ice Cream Sandwich",
    			"Jelly Bean",
    			"KitKat"
    	};
    	
    	private final String[][] mChilds = {
    			{"1.5"},
    			{"1.6"},
    			{"2.0","2.0.1","2.1"},
    			{"2.2","2.2.1","2.2.2","2.2.3"},
    			{"2.3","2.3.1","2.3.2","2.3.3","2.3.4","2.3.5","2.3.6","2.3.7"},
    			{"3.0","3.1","3.2","3.2.1","3.2.2","3.2.3","3.2.4","3.2.5","3.2.6"},
    			{"4.0", "4.0.1", "4.0.2", "4.0.3", "4.0.4"},
    			{"4.1", "4.1.1", "4.1.2", "4.2", "4.2.1", "4.2.2", "4.3", "4.3.1"},
    			{"4.4"}
    	};
    	
    	private View.OnClickListener mFuncMenuClickListener;
    	private Context mContext;
    	
    	private int mCurrFuncPos = -1;
    	
        public MyAdapter(Context ctx) {
            super();
            
            mContext = ctx;
            
            mFuncMenuClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.getId() == R.id.category_item_func) {
                        View categoryRoot = (View) v.getParent().getParent();
                        View funcBar = categoryRoot.findViewById(R.id.category_function);
                        
                        funcBar.setVisibility(View.VISIBLE);
                        mCurrFuncPos = 0;
                        
                        notifyDataSetChanged();
                        
                        Toast.makeText(mContext, "click", Toast.LENGTH_SHORT).show();
                    }
                }
            };
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
    		return mChilds[groupPosition][childPosition];
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
    		return childPosition;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            View v = convertView;
            
            if (v == null) {
                v = mLayoutInfalter.inflate(R.layout.card_item, null);
            }
            
            return v;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
    		return mChilds[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
    		return mGroups[groupPosition];
        }

        @Override
        public int getGroupCount() {
    		return mGroups.length;
        }

        @Override
        public long getGroupId(int groupPosition) {
    		return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
    		View v = convertView;
    		
    		if(v == null) {
    			v = mLayoutInfalter.inflate(R.layout.category_item, null);
    		}
    		
    		View funcBtn = v.findViewById(R.id.category_item_func);
    		funcBtn.setOnClickListener(mFuncMenuClickListener);
    		
    		if (mCurrFuncPos == groupPosition) {
    		    v.findViewById(R.id.category_function).setVisibility(View.VISIBLE);
    		} else {
    		    v.findViewById(R.id.category_function).setVisibility(View.GONE);
    		}
    		
            return v;
        }
        
        @Override
        public void onGroupCollapsed(int groupPosition) {
            if (mCurrFuncPos == groupPosition) {
                mCurrFuncPos = -1;
                notifyDataSetChanged();
            }
            super.onGroupCollapsed(groupPosition);
        }

        @Override
        public void onGroupExpanded(int groupPosition) {
            super.onGroupExpanded(groupPosition);
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }
}